package cn.jcet.mapreduce.flowsum;

import cn.jcet.mapreduce.bean.FlowBean;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @author 鲜磊
 * @Date 2020/12/3 0:14
 */
public class FlowCountMapper extends Mapper<LongWritable,Text,Text,FlowBean> {

    private Text k = new Text();
    private FlowBean v = new FlowBean();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        // 转换为字符串类型
        String line = value.toString();
        // 切割字符串，空格
        String[] fields = line.split("\t");
        // 数据封装
        // 根据数据下标获取，获取手机号码
        k.set(fields[1]);
        // 获取上行流量和下行流量
        long upFlow = Long.parseLong(fields[fields.length - 3]);
        long downFlow = Long.parseLong(fields[fields.length - 2]);
        // 对象封装
        v.setUpFlow(upFlow);
        v.setDownFlow(downFlow);
//        v.set(upFlow,downFlow);

        // 写出
        context.write(k,v);
    }
}
